﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Net;
using System.IO;
using System.Text;
namespace CRMSystemV2.Web
{
    /// <summary>
    /// 所有的返回结果都已json统一格式：{'code':1,'message':'操作成功','data':[]}
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class CRMSystemService : System.Web.Services.WebService
    {
        //将所有的核心逻辑独立成单独的服务，方便多个项目使用
        private readonly CRMSystemAPI.CRMSystemAPIClient api = new CRMSystemAPI.CRMSystemAPIClient();    
    
        #region 登录
        /// <summary>
        /// 用户登录，登录页
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        [ScriptMethod(UseHttpGet = false)]
        public string Login(string username, string password, string dycode)
        {
            ajaxDone json = new ajaxDone();
            if (dycode.ToLower() == Session["CheckCode"].ToString().ToLower())
            {
                //动态代理
                //DynamicProxyFactory factory = new DynamicProxyFactory(apiURL + "?wsdl");
                //DynamicProxy.DynamicProxy proxy = factory.CreateProxy("ICRMSystemAPI");
                //object resultObj = proxy.CallMethod("Login", username, password) ;
                //App_Code.DynamicResult result = new App_Code.DynamicResult(resultObj);
                string result= api.Login(username,password);
                ResultJson rj= result.DeserializeObject();
                json.statusCode = rj.status;
                json.message = rj.message;
                if (rj.status == 200)
                {//保存session
                    Session["username"] = username;
                }
            }
            else
            {
                json.statusCode = 300;
                json.message = "验证码错误";
            }
            return json.SerializeObject();
        } 
        /// <summary>
        /// 用户登录,登录框
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public void LoginDialog(string username, string password, string dycode)
        {
            ajaxDone json = new ajaxDone();
            if (dycode.ToLower() == Session["CheckCode"].ToString().ToLower())
            {
                string result = api.Login(username, password);
                ResultJson rj = result.DeserializeObject();
                json.statusCode = rj.status;
                json.message = rj.message;
                if (rj.status == 200)
                {
                    //保存session
                    Session["username"] = username;
                    json.message = "";//不提示消息
                    json.callbackType = "closeCurrent";
                }
            }
            else
            {
                json.statusCode = 300;
                json.message = "验证码错误";
            }
            json.ResponseWrite();
        }
        #endregion

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public void ChangePassword(string oldPassword, string newPassword)
        {
            string result = api.ChangePassword(Session["username"].ToString(), oldPassword, newPassword);
            ResultJson rj = result.DeserializeObject();            
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                callbackType = "closeCurrent"
            };
            json.ResponseWrite();
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public void UserUpdate(int id,string username,string jobno,string email,string phone,int isenable)
        {
            string result = api.UserUpdate(id, username, jobno, email, phone, isenable);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User",
                callbackType = "closeCurrent"
                //callbackType = "forward",
                //forwardUrl = string.Format("/Sys/User.aspx?pageNum={0}", Session["pageIndex"])                
            };
            json.ResponseWrite();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public void UserInsert(string username, string jobno, string email, string phone, int isenable)
        {
            string result = api.UserInsert(username, jobno, email, phone, isenable);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User",
                callbackType = "closeCurrent",
            };
            json.ResponseWrite();
        }        
    }
    
}
